package cn.org.mrliu.ssm.util.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ValidatorUtil {
	static SimpleDateFormat[] dateFormats = { new SimpleDateFormat("yyyy-MM-dd"),
			new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S"),
			new SimpleDateFormat("yyyy年MM月dd日"), new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"),
			new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒S毫秒"), };

	public static boolean required(String string) {
		if (string == null || string.trim().equals("")) {
			return false;
		}
		return true;
	}

	/**
	 * 数字:整数或小数
	 * 
	 * @param object
	 * @return
	 */
	public static boolean number(Object object) {
		if (object == null || object.toString().trim().equals("")) {
			return false;
		}
		if (!object.toString().trim().matches("^[+-]?\\d{1,}|[+-]?\\d{1,}\\.\\d{1,}$")) {
			return false;
		}
		return true;
	}

	/**
	 * 数字:整数
	 * 
	 * @param object
	 * @return
	 */
	public static boolean digits(Object object) {
		if (object == null || object.toString().trim().equals("")) {
			return false;
		}
		if (!object.toString().matches("^[+-]?\\d+$")) {
			return false;
		}
		return true;
	}

	public static boolean length(String string, int length) {
		if (string == null || string.trim().equals("")) {
			return false;
		}
		if (string.trim().length() != length) {
			return false;
		}
		return true;
	}

	public static boolean maxLength(String string, int maxLength) {
		if (string == null) {
			return true;
		}
		if (string.trim().length() > maxLength) {
			return false;
		}
		return true;
	}

	public static boolean minLength(String string, int minLength) {
		if (string == null || string.trim().equals("")) {
			return false;
		}
		if (string.trim().length() < minLength) {
			return false;
		}
		return true;
	}

	public static boolean rangeLength(String string, int minLength, int maxLength) {
		if (string == null || string.trim().equals("")) {
			return false;
		}
		if (string.trim().length() < minLength || string.trim().length() > maxLength) {
			return false;
		}
		return true;
	}

	public static boolean max(Object object, double max) {
		if (object == null || object.toString().trim().equals("")) {
			return false;
		}
		if (!number(object)) {
			return false;
		}
		Double d = Double.parseDouble(object.toString().trim());
		if (d > max) {
			return false;
		}
		return true;
	}

	public static boolean min(Object object, double min) {
		if (object == null || object.toString().trim().equals("")) {
			return false;
		}
		if (!number(object)) {
			return false;
		}
		Double d = Double.parseDouble(object.toString().trim());
		if (d < min) {
			return false;
		}
		return true;
	}

	public static boolean range(Object object, int min, int max) {
		if (object == null || object.toString().trim().equals("")) {
			return false;
		}
		if (!number(object)) {
			return false;
		}
		Double d = Double.parseDouble(object.toString().trim());
		if (d < min || d > max) {
			return false;
		}
		return true;
	}

	public static boolean equalTo(Object object1, Object object2) {
		if (object1 == null && object2 == null) {
			return true;
		}
		if (object1 == null) {
			return false;
		}
		if (object2 == null) {
			return false;
		}
		if (!object1.toString().trim().equals(object2.toString().trim())) {
			return false;
		}
		return true;
	}

	public static boolean accept(String string, String... suffixs) {
		if (string == null || string.trim().equals("")) {
			return false;
		}
		if (suffixs == null || suffixs.length == 0) {
			return false;
		}
		boolean flag = false;
		string = string.trim();
		for (String suffix : suffixs) {
			if (string.endsWith(suffix)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	public static boolean phone(String url) {

		return true;
	}

	public static boolean telPhone(String url) {

		return true;
	}

	public static boolean qq(String url) {

		return true;
	}

	public static boolean url(String url) {

		return true;
	}

	public static boolean email(String email) {

		return true;
	}

	public static boolean idCard(String idCard) {

		return true;
	}

	public static boolean bankCard(String bankCard) {

		return true;
	}

	// 邮编
	public static boolean postCode(String postCode) {

		return true;
	}

	public static boolean date(String dateString) {
		if (dateString == null || dateString.trim().equals("")) {
			return false;
		}
		dateString = dateString.trim();
		if (dateString.length() < 9) {
			return false;
		}
		String year = dateString.substring(0, 4);
		if (!year.matches("^\\d{4}$")) {
			return false;
		}
		boolean flag = false;
		for (SimpleDateFormat simpleDateFormat : dateFormats) {
			try {
				Date date = simpleDateFormat.parse(dateString);
				System.out.println(date);
				System.out.println(date.getTime());
				flag = true;
				break;
			} catch (ParseException e) {
				// e.printStackTrace();
			}
		}
		return flag;
	}

	public static boolean date(String dateString, String pattern) {
		if (dateString == null || dateString.trim().equals("")) {
			return false;
		}
		if (pattern == null || pattern.trim().equals("")) {
			return date(dateString);
		}
		boolean flag = false;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		Date date;
		try {
			date = simpleDateFormat.parse(dateString);
			flag = true;
			System.out.println(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public static boolean rangeDate(Date date,String startDateString,String endDateString) {
		if (date==null) {
			return false;
		}
		if (startDateString==null||startDateString.trim().equals("")) {
			return false;
		}
		if (endDateString==null||endDateString.trim().equals("")) {
			return false;
		}
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date startDate = dateFormat.parse(startDateString);
			Date endDate = dateFormat.parse(endDateString);
			long time = date.getTime();
			long startTime = startDate.getTime();
			long endTime = endDate.getTime();
			if (time < startTime || time > endTime) {
				return false;
			}
			return true;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
	public static boolean rangeDate(Date date,String startDateString,String endDateString,String pattern) {
		if (date==null) {
			return false;
		}
		if (startDateString==null||startDateString.trim().equals("")) {
			return false;
		}
		if (endDateString==null||endDateString.trim().equals("")) {
			return false;
		}
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		try {
			Date startDate = dateFormat.parse(startDateString);
			Date endDate = dateFormat.parse(endDateString);
			long time = date.getTime();
			long startTime = startDate.getTime();
			long endTime = endDate.getTime();
			if (time < startTime || time > endTime) {
				return false;
			}
			return true;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
	public static boolean rangeDate(Date date,Date startDate,Date endDate) {
		if (date==null) {
			return false;
		}
		if (startDate==null) {
			return false;
		}
		if (endDate==null) {
			return false;
		}
		long time = date.getTime();
		long startTime = startDate.getTime();
		long endTime = endDate.getTime();
		if (time < startTime || time > endTime) {
			return false;
		}
		return true;
	}

	
}
